package com.sangfor.ssl.l3vpn.service.networkInfo;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/* loaded from: classes2.dex */
public class AppInfo {
    private static final String TAG = "AppInfo";
    public final String allAppName;
    public final String leaderAppName;
    public final PackageNames pkgs;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes2.dex */
    public static class Entry {
        final String appName;
        final String pkgName;

        public Entry(String str, String str2) {
            this.appName = str;
            this.pkgName = str2;
        }
    }

    private AppInfo(String str, String str2, String[] strArr) {
        this.leaderAppName = str;
        this.allAppName = str2;
        this.pkgs = PackageNames.newInstance(strArr);
    }

    public static AppInfo createFromUid(Context context, int i) {
        String[] packagesForUid;
        Log.info(TAG, "createFromUid uid is " + i);
        PackageManager packageManager = context.getPackageManager();
        ArrayList arrayList = new ArrayList();
        if (i > 0) {
            try {
                for (String str : packageManager.getPackagesForUid(i)) {
                    if (str != null) {
                        try {
                            PackageInfo packageInfo = packageManager.getPackageInfo(str, 0);
                            String charSequence = packageInfo != null ? packageInfo.applicationInfo.loadLabel(packageManager).toString() : null;
                            if (charSequence == null || charSequence.equals("")) {
                                charSequence = str;
                            }
                            arrayList.add(new Entry(charSequence, str));
                        } catch (PackageManager.NameNotFoundException unused) {
                            arrayList.add(new Entry(str, str));
                        }
                    }
                }
            } catch (RuntimeException unused2) {
                Log.info(TAG, "error getPackagesForUid(). package manager has died");
                return null;
            }
        } else if (i != 0) {
            return null;
        } else {
            arrayList.add(new Entry("System", "root.uid=0"));
        }
        Collections.sort(arrayList, new Comparator<Entry>() { // from class: com.sangfor.ssl.l3vpn.service.networkInfo.AppInfo.1
            @Override // java.util.Comparator
            public int compare(Entry entry, Entry entry2) {
                int compareToIgnoreCase = entry.appName.compareToIgnoreCase(entry2.appName);
                return compareToIgnoreCase == 0 ? entry.pkgName.compareToIgnoreCase(entry2.pkgName) : compareToIgnoreCase;
            }
        });
        String[] strArr = new String[arrayList.size()];
        String[] strArr2 = new String[arrayList.size()];
        for (int i2 = 0; i2 < arrayList.size(); i2++) {
            strArr[i2] = ((Entry) arrayList.get(i2)).pkgName;
            strArr2[i2] = ((Entry) arrayList.get(i2)).appName;
            Log.info(TAG, "createFromUid uid is " + i + "pkg name is " + strArr[i2] + "appname is " + strArr2[i2]);
        }
        return new AppInfo(strArr2[0], TextUtils.join(",", strArr2), strArr);
    }
}
